﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOHVote.BLL;
using BOHVote.Entity;
 

namespace BOH.AccreditationSystem
{
    public partial class InterviewQuestionModify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"] != null)
                {
                    BHUser bhUser = (BHUser)Session["User"];
                    LoadDrop(bhUser.RightCode);
                    //SpecialityBind(bhUser.RightCode);
                }

                if (Request.QueryString["QID"] != null && Request.QueryString["QID"].ToString().Trim().Length > 0)
                {
                    FillForme(Request.QueryString["QID"].ToString());
                }
            }
        }

        #region << bind drop down list  >>
        public void LoadDrop(string subjectID)
        {
            SubjectBind("", subjectID);
            LevelQuestion();
        }
        public void LevelQuestion()
        {
            DataSet dst = OtherClass.getJobRanks("9");
            ListItem liN = new ListItem("请选择","");
            ListItem liH = new ListItem("全部正高", "0");
            drpLevel.Items.Add(liN);
            drpLevel.Items.Add(liH);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem li = new ListItem(dr["JobRankName"].ToString(), dr["JobRankID"].ToString());
                drpLevel.Items.Add(li);
            }
            DataSet dstA = OtherClass.getJobRanks("6");
            ListItem liA = new ListItem("全部副高","1");
            drpLeveLA.Items.Add(liN);
            drpLeveLA.Items.Add(liA);
            foreach (DataRow dr in dstA.Tables[0].Rows)
            {
                ListItem li = new ListItem(dr["JobRankName"].ToString(), dr["JobRankID"].ToString());
                drpLeveLA.Items.Add(li);
            }
        }
        public void SubjectBind(string level,string subjectID)
        {
            this.drpSubject.Items.Clear();
            DataSet dst = null;
            string sbujectName = string.Empty;
            if (String.IsNullOrEmpty(level) && String.IsNullOrEmpty(subjectID))
                dst = Subject.getAllASubject();
            else if (String.IsNullOrEmpty(subjectID))
                dst = Subject.getSubjectByLevel(level);
            if (!String.IsNullOrEmpty(subjectID))
            {
                sbujectName = Subject.getSubjectNameBySubjectID(subjectID);
            }
            ListItem li1 = new ListItem("请选择学科组", "-1");
            this.drpSubject.Items.Add(li1);
            if (dst != null)
            {
                foreach (DataRow dr in dst.Tables[0].Rows)
                {
                    if (dr["SubjectID"].ToString().Length > 0)
                    {
                        ListItem li = new ListItem(dr["SubjectName"].ToString(), dr["SubjectID"].ToString());
                        this.drpSubject.Items.Add(li);
                    }
                }
            }
            if (!String.IsNullOrEmpty(sbujectName))
            {
                ListItem lisubject = new ListItem(sbujectName, subjectID);
                this.drpSubject.Items.Add(lisubject);
            }
        }

        protected void drpSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strSubjectID = drpSubject.SelectedValue;
            if (strSubjectID != "-1")
            {
                TestSpecialityBind(strSubjectID);
            }
            else
            {
                drpTestSpeciality.Items.Clear();
                ListItem li1 = new ListItem("共通", "0");
                this.drpTestSpeciality.Items.Add(li1);
            }
        }

        public void TestSpecialityBind(string SubjectID)
        {
            drpTestSpeciality.Items.Clear();
            ListItem li1 = new ListItem("全部专业[共性题]", "0");
            this.drpTestSpeciality.Items.Add(li1);

            DataSet dst = Subject.getAllTestSpecialityBySubjectID(SubjectID);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem li = new ListItem(dr["TestSpecialityName"].ToString(), dr["TestSpecialityID"].ToString());
                this.drpTestSpeciality.Items.Add(li);
            }
        }

        public void BindDropDownList(ListControl Controls, DataSet dst)
        {
            Controls.Items.Clear();
            ListItem list = new ListItem("请选择", "-1");
            Controls.Items.Add(list);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem itemlist = new ListItem(dr["dict_Display"].ToString(), dr["dict_Value"].ToString());
                Controls.Items.Add(itemlist);
            }
        }
        #endregion

        public void FillForme(string StrQID)
        {
            DataSet dst = InterviewClass.getQuestionByID(StrQID);
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                txtQuestions.Value = dst.Tables[0].Rows[0]["Question_Content"].ToString();
                //txtQuestionTitle.Text = dst.Tables[0].Rows[0]["Question_Title"].ToString();
                QuestionAnswer.Value = dst.Tables[0].Rows[0]["Question_Answer"].ToString();
                drpSubject.SelectedValue = dst.Tables[0].Rows[0]["Question_Subject"].ToString();
                drpSubject_SelectedIndexChanged(null, null);
                //txtSubSpeciality.Text = dst.Tables[0].Rows[0]["Question_SubSpecialty"].ToString();
                //if (dst.Tables[0].Rows[0]["Question_Specialty"].ToString().Trim() == "0")
                //{
                //    ckbCommon.Checked = true;
                //    drpSpeciality.Enabled = false;
                //}
                //else
                //{
                //    ckbCommon.Checked = false;
                    drpTestSpeciality.SelectedValue = dst.Tables[0].Rows[0]["Question_Specialty"].ToString();
                //}
                    if (dst.Tables[0].Rows[0]["Question_JobRank"].ToString() == "0")
                        drpLevel.SelectedValue = "0";
                    else if (dst.Tables[0].Rows[0]["Question_JobRank"].ToString() == "1")
                        drpLeveLA.SelectedValue = "1";
                    else
                    {
                        string Level = OtherClass.getJobRankLevelByJobRanK(dst.Tables[0].Rows[0]["Question_JobRank"].ToString());
                        if (Level == "9")
                            drpLevel.SelectedValue = dst.Tables[0].Rows[0]["Question_JobRank"].ToString();
                        else if (Level == "6")
                            drpLeveLA.SelectedValue = dst.Tables[0].Rows[0]["Question_JobRank"].ToString();
                    }
                
                
                //if (dst.Tables[0].Rows[0]["Question_Subject"].ToString().ToUpper() == "A")
                //    IsActive.Checked = true;
                //else
                //    IsActive.Checked = false;
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            if (txtQuestions.Value.Trim().Length>0)
            {
                string strSpeciality = string.Empty;
                //if(ckbCommon.Checked)
                //    strSpeciality ="0";
                //else
                    strSpeciality = drpTestSpeciality.SelectedValue;
                //string strActive = string.Empty;
                //if (IsActive.Checked)
                //    strActive = "A";
                //else
                //    strActive = "D";
                string JobLevel = string.Empty;
                if (!String.IsNullOrEmpty(drpLevel.SelectedValue))
                    JobLevel = drpLevel.SelectedValue.Trim();
                else if (!String.IsNullOrEmpty(drpLeveLA.SelectedValue))
                    JobLevel = drpLeveLA.SelectedValue.Trim();
                else
                    JobLevel = "-1";
                if (Request.QueryString["QID"] != null && Request.QueryString["QID"].ToString().Trim().Length > 0)
                    InterviewClass.UpdateInterview(Request.QueryString["QID"].ToString().Trim(), string.Empty, txtQuestions.Value.Trim(), QuestionAnswer.Value, Convert.ToInt32(JobLevel), drpSubject.SelectedValue, strSpeciality, "");
                else
                    InterviewClass.SaveInterview(string.Empty, txtQuestions.Value.Trim(), QuestionAnswer.Value, Convert.ToInt32(JobLevel), drpSubject.SelectedValue, strSpeciality, "", (string)Session["ApplyPeriod"]);
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('请输入必要的信息!');</script>");
            }
        }
    }
}
